package youcaihuaoa.weetop.com.ui.main

import android.graphics.Color
import android.location.Geocoder
import android.os.Bundle
import android.util.Log
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.fastjson.JSON
import com.robot.base.base.activity.GLBaseActivity
import com.robot.base.configs.AppConstants
import youcaihuaoa.weetop.com.BR
import youcaihuaoa.weetop.com.R
import youcaihuaoa.weetop.com.databinding.AppActivitySignBinding
import youcaihuaoa.weetop.com.databinding.AppActivitySignMapBinding
import youcaihuaoa.weetop.com.vm.main.SignMapViewModel
import youcaihuaoa.weetop.com.vm.main.SignViewModel
import com.amap.api.maps2d.model.MyLocationStyle
import com.amap.api.maps2d.AMap
import com.amap.api.maps2d.CameraUpdateFactory
import com.amap.api.maps2d.MapView
import com.amap.api.maps2d.model.Circle
import com.amap.api.maps2d.model.CircleOptions
import com.amap.api.maps2d.model.LatLng
import com.blankj.utilcode.util.LogUtils
import com.robot.base.model.SignGroupBean
import java.util.*


@Route(path = AppConstants.Router.Main.A_SIGN_MAP)
class SignMapActivity : GLBaseActivity<AppActivitySignMapBinding, SignMapViewModel>() {

    private lateinit var aMap: AMap

    private lateinit var mapView: MapView

    var signProfileBean: SignGroupBean? = null

    var circle: Circle? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding.map.onCreate(savedInstanceState);
    }

    override fun initComponents() {
        addSingleTitleBar("考勤打卡")
        mapView = binding.map
        aMap = binding.map.map
        val myLocationStyle: MyLocationStyle =
            MyLocationStyle() //初始化定位蓝点样式类myLocationStyle.myLocationType(MyLocationStyle.LOCATION_TYPE_LOCATION_ROTATE);//连续定位、且将视角移动到地图中心点，定位点依照设备方向旋转，并且会跟随设备移动。（1秒1次定位）如果不设置myLocationType，默认也会执行此种模式。
        myLocationStyle.myLocationType(MyLocationStyle.LOCATION_TYPE_FOLLOW);//连续定位、且将视角移动到地图中心点，定位蓝点跟随设备移动。（1秒1次定位）
        myLocationStyle.interval(2000) //设置连续定位模式下的定位间隔，只在连续定位模式下生效，单次定位模式下不会生效。单位为毫秒。
        val isOpenAddress = signProfileBean?.kqz_data?.address_status == "1"
        aMap.setMyLocationStyle(myLocationStyle) //设置定位蓝点的Style
        aMap.isMyLocationEnabled = true // 设置为true表示启动显示定位蓝点，false表示隐藏定位蓝点并不进行定位，默认是false
        aMap.setOnMyLocationChangeListener {
            //获取经纬度
            aMap.moveCamera(CameraUpdateFactory.zoomTo(17f));
            val currentLat = it.latitude
            val currentLng = it.longitude
//            val currentLat = (signProfileBean?.kqz_data?.address_lat?:0).toDouble()
//            val currentLng = (signProfileBean?.kqz_data?.address_lng?:0).toDouble()
            //119.654532  //27.947091

            val geoCoder = Geocoder(this, Locale.getDefault())
            val address = geoCoder.getFromLocation(currentLat, currentLng, 1)
            val address_data = address[0].getAddressLine(0)
            LogUtils.d("SignMap-----${address_data}-")
            val centerLatLng = LatLng(currentLat, currentLng)
            if (isOpenAddress) {
                viewModel.checkIsCanSign(
                    circle?.contains(centerLatLng) == true,
                    currentLat.toString(),
                    currentLng.toString(),
                    address_data
                )
            } else {
                viewModel.checkIsCanSign(
                    true,
                    currentLat.toString(),
                    currentLng.toString(),
                    address_data
                )
            }
        }
        if (isOpenAddress) {
            val lng: Double = signProfileBean?.kqz_data?.address_lng ?: 0.0
            val lat: Double = signProfileBean?.kqz_data?.address_lat ?: 0.0
            val space: Double = signProfileBean?.kqz_data?.address_space ?: 0.0
            //圆形
            //lat 29.909235  lng112.085719
            val centerLatLng = LatLng(lat, lng);

//            val centerLatLng = LatLng(29.909235, 112.085719);
            val circleOptions = CircleOptions();
            //属性信息
            circleOptions.center(centerLatLng)
                .center(centerLatLng)
                .radius(space) //半径范围
                .strokeColor(Color.parseColor("#2886ff"))
                .strokeWidth(5f)
                .fillColor(Color.parseColor("#1a2886ff"));
            circle = aMap.addCircle(circleOptions);
//            LogUtils.e()
        }


        viewModel.initSignProfile(signProfileBean)
    }

    override fun initParam() {
        val signProfileJson = intent.getStringExtra("signProfile")
        signProfileBean = JSON.parseObject(signProfileJson, SignGroupBean::class.java)
    }


    override fun initContentView(savedInstanceState: Bundle?): Int =
        R.layout.app_activity_sign_map

    override fun initVariableId(): Int = BR.viewModel

    override fun onDestroy() {
        super.onDestroy()
        mapView.onDestroy()
    }

    override fun onResume() {
        super.onResume()
        mapView.onResume()
    }

    override fun onPause() {
        super.onPause()
        mapView.onPause()
    }

    override fun onSaveInstanceState(outState: Bundle) {
        super.onSaveInstanceState(outState)
        mapView.onSaveInstanceState(outState);
    }
}